package com.xlh.service.courseTest.impl;

import com.google.common.collect.Lists;
import com.xlh.dao.courseTest.CourseQuestionRepositoryDetailsMapper;
import com.xlh.dao.courseTest.CourseQuestionRepositoryDetailsMapperExt;
import com.xlh.enums.FileTypeEnum;
import com.xlh.pojo.courseTest.CourseQuestionRepositoryDetails;
import com.xlh.pojo.courseTest.CourseQuestionRepositoryDetailsExample;
import com.xlh.service.courseTest.QuestionRepositoryDetailsService;
import com.xlh.util.BeanUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhr 2019/3/16
 */
@Service
public class QuestionRepositoryDetailsServiceImpl implements QuestionRepositoryDetailsService {
    @Autowired
    private CourseQuestionRepositoryDetailsMapper mapper;
    @Autowired
    private CourseQuestionRepositoryDetailsMapperExt mapperExt;

    @Override
    public Map<Long, List<CourseQuestionRepositoryDetails>> getDetailsMap(List<Long> questionIdList, FileTypeEnum fileTypeEnum) {
        List<CourseQuestionRepositoryDetails> list = getDetailsList(questionIdList, fileTypeEnum);
        return BeanUtil.aggByKeyToList("questionId", list);
    }

    @Override
    public List<CourseQuestionRepositoryDetails> getDetailsList(List<Long> questionIdList, FileTypeEnum fileTypeEnum) {
        boolean isHtml = Objects.equals(fileTypeEnum, FileTypeEnum.HTML);
        List<CourseQuestionRepositoryDetails> list = selectDetailsByQuestionIds(questionIdList);
        list.forEach(details -> {
            try {
                details.setContent(isHtml ? details.getHtmlContent() : details.getMdContent());
            } catch (Exception e) {
                details.setContent(null);
            }
        });
        return list;
    }

    @Override
    public Map<Long, List<CourseQuestionRepositoryDetails>> getDetailsMap(List<Long> questionIds) {
        List<CourseQuestionRepositoryDetails> list = selectDetailsByQuestionIds(questionIds);
        return BeanUtil.aggByKeyToList("questionId", list);
    }

    @Override
    public void cleanData() {
        mapperExt.clean();
    }

    @Override
    public List<CourseQuestionRepositoryDetails> selectByQuestionId(Long questionId) {
        CourseQuestionRepositoryDetailsExample example = new CourseQuestionRepositoryDetailsExample();
        example.setOrderByClause("QUESTION_ID ASC, ORDER_NUMBER ASC");
        example.createCriteria().andQuestionIdEqualTo(questionId);
        return mapper.selectByExampleWithBLOBs(example);
    }

    @Override
    public Long selectMaxId() {
        return mapperExt.selectMaxId();
    }

    @Override
    public void updateDelete(List<Long> questionIdList) {
        // 设置where条件
        CourseQuestionRepositoryDetailsExample example = new CourseQuestionRepositoryDetailsExample();
        example.createCriteria().andQuestionIdIn(questionIdList);

        // 设置删除状态
        CourseQuestionRepositoryDetails updateDetailsDelMarkModel = new CourseQuestionRepositoryDetails();
        updateDetailsDelMarkModel.setDelMark(true);

        // 执行删除操作
        mapper.updateByExampleSelective(updateDetailsDelMarkModel, example);
    }

    @Override
    public void insertSelective(CourseQuestionRepositoryDetails details) {
        mapper.insertSelective(details);
    }

    @Override
    public void insertBatch(List<CourseQuestionRepositoryDetails> answerList) {
        if (CollectionUtils.isNotEmpty(answerList)) mapperExt.insertBatch(answerList);
    }

    @Override
    public void deleteByQuestionId(Long questionId) {
        CourseQuestionRepositoryDetailsExample deleteDetailsExample = new CourseQuestionRepositoryDetailsExample();
        deleteDetailsExample.createCriteria().andQuestionIdEqualTo(questionId);
        mapper.deleteByExample(deleteDetailsExample);
    }

    private List<CourseQuestionRepositoryDetails> selectDetailsByQuestionIds(List<Long> questionIdList) {
        if (CollectionUtils.isNotEmpty(questionIdList)) {
            CourseQuestionRepositoryDetailsExample example = new CourseQuestionRepositoryDetailsExample();
            example.setOrderByClause("QUESTION_ID ASC, ORDER_NUMBER ASC");
            example.createCriteria().andQuestionIdIn(questionIdList);
            return mapper.selectByExampleWithBLOBs(example);
        }
        return Lists.newArrayList();
    }
}
